package cn.bestick.action;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import cn.bestick.dao.ClassDao;
import cn.bestick.dao.GradeDao;
import cn.bestick.model.Grade;
import cn.bestick.util.DbUtil;
import cn.bestick.util.NavUtil;
import cn.bestick.util.ResponseUtil;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年9月8日 下午4:54:03
 */
public class GradeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private GradeDao gradeDao = new GradeDao();

	private ClassDao classDao = new ClassDao();
	
	private List<Grade> gradeList = new ArrayList<Grade>();

	private String mainPage;

	private String navCode;
	
	private Grade grade;
	
	private Integer gradeId;

	public Grade getGrade() {
		return grade;
	}

	public void setGrade(Grade grade) {
		this.grade = grade;
	}

	public Integer getGradeId() {
		return gradeId;
	}

	public void setGradeId(Integer gradeId) {
		this.gradeId = gradeId;
	}

	public List<Grade> getGradeList() {
		return gradeList;
	}

	public void setGradeList(List<Grade> gradeList) {
		this.gradeList = gradeList;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public String getNavCode() {
		return navCode;
	}

	public void setNavCode(String navCode) {
		this.navCode = navCode;
	}

	public String list() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			gradeList = gradeDao.gradeList(con);
			navCode = NavUtil.getNavgation("年级信息管理", "年级维护");
			mainPage = "grade/gradeList.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	public String preSave() {
		if (gradeId!=null) {
			Connection con = null;
			try {
				con = DbUtil.getInstance().getCon();
				grade = gradeDao.getGradeById(con, gradeId);
				navCode = NavUtil.getNavgation("年级信息管理", "年级修改");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					DbUtil.getInstance().closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {
			navCode = NavUtil.getNavgation("年级信息管理", "年级添加");
		}
		mainPage = "grade/gradeSave.jsp";
		return SUCCESS;
	}

	public String save() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			if (gradeId!=null) {
				grade.setGradeId(gradeId);
				gradeDao.gradeUpdate(con, grade);
			} else {
				gradeDao.gradeAdd(con, grade);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "save";
	}

	public String delete() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			JSONObject result = new JSONObject();
			boolean exist = classDao.existClassById(con, gradeId);
			if (exist) {
				result.put("error", "年级下存在班级，不能删除！");
			} else {
				result.put("success", "删除成功！");
				gradeDao.gradeDelete(con, gradeId);
			}
			ResponseUtil.write(result, ServletActionContext.getResponse());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
